package Classic150;

import java.util.Arrays;

public class H指数 {
    public int hIndex(int[] citations) {
        /*
            计数排序，累加获值
         */
        int len = citations.length;
        int[] counter = new int[len + 1];
        for (int citation : citations) {
            if (citation >= len) counter[len]++;
            else counter[citation]++;
        }
        int sum = 0;
        for (int i = len; i >= 0; i--) {
            sum += counter[i];
            if (sum >= i) return i;
        }
        return 0;

        /*
            排序，逆向遍历
         */
        /*Arrays.sort(citations);
        int h = 0;
        for (int i = citations.length - 1; i >= 0 && citations[i] > h; i--) h++;
        return h;*/
    }
}
